package com.cskaoyan.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @author stone
 * @date 2022/05/31 11:22
 */
@RestController
public class HelloController {

    //localhost:8080/songge/article/details/110
    //localhost:8080/ligenli/article/details/120
    //localhost:8080/xuejia/article/details/10086
    //{}里的就是占位符的名字，通过占位符的名称获得对应的值
    //通过@PathVariable注解的value属性值获得指定占位符的值，给到这个注解所对应的形参
    @RequestMapping("{username}/article/details/{id}")
    public String getUrl(@PathVariable("username") String name,
                         @PathVariable("id") Integer id) {
        System.out.println(name + ":" + id);
        return "ok";
    }

    // 如果使用了@RequestParam注解，一定要携带对应的参数
    //localhost:8080/login?username=songge&password=123456
    @RequestMapping("login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password) {
        return "ok";
    }

    // 使用@RequestHeader获得指定请求头的值
    // 可以使用字符串或字符串数组（将字符串转换为字符串数组 → 根据逗号来分割）来接收
    @RequestMapping("header")
    public String header(@RequestHeader("Host") String host,
                         @RequestHeader("Accept") String[] accept) {
        System.out.println("host = " + host);
        System.out.println("accept = " + Arrays.asList(accept));

        return "ok";

    }

    // 使用@CookieValue来获得指定name的cookie的value
    @RequestMapping("cookie")
    public String cookie(@CookieValue("songge") String value) {
        System.out.println("value = " + value);
        return "ok";
    }

    // 向session中的username放入一个值
    @RequestMapping("session/put/{value}")
    public String putSession(@PathVariable("value") String value, HttpSession session) {
        //User user = new User();
        session.setAttribute("username", value);
        return "ok";
    }

    // 使用@SessionAttribute来获得指定Session中的值
    @RequestMapping("session/get")
    public String getSession(@SessionAttribute("username") String object) {
        //session.setAttribute("username", value);放入的值的类型是什么，形参就可以使用什么类型来接收
        System.out.println("object = " + object);
        return "ok";
    }

}
